//
//  ITCropImageController.m
//  InTubePro
//
//  Created by seven on 2020/5/12.
//  Copyright © 2020 seven. All rights reserved.
//

#import "ITCropImageController.h"
#import "ITCropImageView.h"

@interface ITCropImageController (){
    ITCropImageView *_cropView;
}

@property (nonatomic, assign) CGRect cropFrame;
@property (nonatomic, assign) CGFloat cropRatio;
@property (nonatomic, retain) UIImage *originalImage;
@property (nonatomic, assign) BOOL isCycle;
@end

@implementation ITCropImageController


- (id)initWithImage:(UIImage *)originalImage cropRatio:(CGFloat)cropRatio isCycle:(BOOL)isCycle {
    if (self = [super init])
        {
        self.cropRatio = cropRatio;
        self.originalImage = originalImage;
        self.isCycle = isCycle;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.backBtn.hidden = YES;
    self.titleLbl.text = @"Move and Scale";
    [self.gradientLayer removeFromSuperlayer];
    //self.view.backgroundColor = COLOR_BLACK;
    CGFloat top = self.topView.bottom;
    CGFloat bottom = 70;
    _cropView = [ITCropImageView.alloc initWithFrame:CGRectMake(0, top, KScreenWidth, kScreenHeight - bottom - top)];
    _cropView.backgroundColor = COLOR_CLEAR;
    _cropView.clipsToBounds = YES;
    _cropView.showMidLines = YES;
    _cropView.needScaleCrop = NO;
    _cropView.autoScaleCenter = YES;
    //_cropView.showCrossLines = YES;
    _cropView.cropAreaCornerWidth = 20;
    _cropView.cropAreaCornerHeight = 20;
    _cropView.minSpace = _cropView.cropAreaCornerHeight * 3;
    _cropView.cropAreaCornerLineColor = [UIColor whiteColor];
    _cropView.cropAreaBorderLineColor = [UIColor whiteColor];
    
    _cropView.cropAreaCornerLineWidth = 2;
    _cropView.cropAreaBorderLineWidth = 1;
    
    UIColor *lineColor = [UIColor colorWithWhite:1 alpha:0.7];
    _cropView.cropAreaMidLineWidth = 100;
    _cropView.cropAreaMidLineHeight = 2;
    _cropView.cropAreaMidLineColor = lineColor;
    _cropView.cropAreaCornerLineColor = lineColor;
    _cropView.cropAreaCrossLineColor = lineColor;
    _cropView.cropAreaCrossLineWidth = .5;
    
    _cropView.cornerBorderInImage = YES;
    
    _cropView.maskColor = [UIColor colorWithWhite:0 alpha:0.7];
    
    _cropView.tmpCornerMargin = 20;
    
    if (_isCycle) {
        _cropView.cycleMaskEnable = YES;
    }else {
        _cropView.showCorners = YES;
        //_cropView.cropAspectRatio = 1;//0 可变比  其他固定次比例
        //_cropView.currCropAspectRatio = 1;//当前初始比例
    }
    [_cropView setToCropImage:_originalImage];
    //_cropView.currCropAspectRatio = _cropRatio;
    _cropView.cropAspectRatio = _cropRatio;

    [_cropView setImageViewFrame:_cropView.bounds];
    
    [self.view addSubview:_cropView];
    
    [self initControlBtn];
    // Do any additional setup after loading the view.
}


- (void)initControlBtn {
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 70.0f, self.view.frame.size.width, 70)];
    backView.backgroundColor = [UIColor colorWithRed:40/255.f green:40/255.f blue:40/255.f alpha:0.8];
    
    UIButton *cancelBtn = [self buttonWithTitle:[AppUtils titleForKey:@"cancel"]];
    cancelBtn.frame = CGRectMake(0, 10, 100, 50);
    [cancelBtn addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:cancelBtn];
    
    UIButton *confirmBtn = [self buttonWithTitle:[AppUtils titleForKey:@"comfirm"]];
    confirmBtn.frame = CGRectMake(self.view.frame.size.width - 100.0f, 10, 100, 50);
    [confirmBtn addTarget:self action:@selector(confirm:) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:confirmBtn];
    [self.view addSubview:backView];
}

- (UIButton *)buttonWithTitle:(NSString *)title{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor clearColor];
    [button setTitle:title forState:UIControlStateNormal];
    [button.titleLabel setFont:[UIFont boldSystemFontOfSize:18.0f]];
    [button.titleLabel setTextAlignment:NSTextAlignmentCenter];
    [button.titleLabel setLineBreakMode:NSLineBreakByWordWrapping];
    [button.titleLabel setNumberOfLines:0];
    [button setTitleEdgeInsets:UIEdgeInsetsMake(5.0f, 5.0f, 5.0f, 5.0f)];
    return button;
}
#pragma mark - Action

- (void)confirm:(id)sender {
    self.submitblock(self, [_cropView.imageView outputImage]);
}

- (void)cancel:(id)sender {
    self.cancelblock(self);
}

@end
